package com.ikkong.qy.controller;

import java.io.ObjectOutputStream.PutField;
import java.util.List;
import java.util.Map;

import com.ikkong.biz.model.Parent;
import com.ikkong.biz.model.Student;
import com.ikkong.biz.model.Teacher;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.qy.core.WeixinBaseController;
import com.ikkong.qy.util.WeixinApi;
import com.ikkong.system.model.Dept;
import com.jfinal.core.Controller;

public class WeixinContactController extends WeixinBaseController {

	public void listDept() {
		Integer deptId = getParaToInt("deptId", 0);
		Integer type = getUserType();
		System.out.println("listDEPT：用户类型："+type);
		switch (type) {
		case 1: // 老师，查看同部门老师以及所带的班级
			if (deptId == 0) {

				Integer userId = getUser().getId();
				Teacher teacher = Blade.create(Teacher.class).findById(userId);
				String depts = JStrKit.removeWrapWithDelimiter(teacher.getDepts());

				List<Map> mapList = Db.init().queryListMap(
						"select d.id,d.fullname as name,d.pid,d.type from tfw_dept d where find_in_set(d.id,get_dept_childrens('"
								+ depts + "')) > 0");
				// 老师部门
				// 学生和家长部门
				List<Map> parMapList = Db
						.init()
						.queryListMap(
								"select d.id,d.fullname as name,d.pid,d.type from tfw_dept d,tb_classes c where d.type in (2,3) and (d.id = c.stu_dept_id or d.id = c.parent_dept_id) and (c.head_teacher = "
										+ userId + " or c.teachers like '%," + userId + ",%')");
				if (parMapList != null && parMapList.size() > 0) {
					mapList.addAll(parMapList);
					parMapList = Db.init().queryListMap(
							"select d.id,d.fullname as name,d.pid,4 as type from tfw_dept d where d.id in (1001,1002)");
					mapList.addAll(parMapList);
				}
				//
				setAttr("nodeList", mapList);
				render("/weixin/contact/list.html");

			} else {

				render("/weixin/contact/personlist.html");
			}
			return;
		case 2: {
			Parent parent = (Parent) getUser();
			List<Map> teacherList = Db
					.init()
					.queryListMap(
							"select t.id, t.name, 1 as type, t.position, t.mobile from tb_teacher t,tb_classes c where (c.head_teacher = t.id or c.teachers like concat('%,',t.id,',%')) and c.parent_dept_id = "
									+ parent.getDept_id());

			List<Map> mapList = Db
					.init()
					.queryListMap(
							"select p.id,2 as type, p.name,p.mobile, case p.position when 1 then '父亲' when 2 then '母亲' else '其他联系人' end as position,p.stu_name as sname from tb_parent p where p.dept_id = "
									+ parent.getDept_id());

			teacherList.addAll(mapList);
			setAttr("personList", teacherList);

			render("/weixin/contact/personlist.html");
		}
			break;
		case 3: {
			Student student = (Student) getUser();
			List<Map> teacherList = Db
					.init()
					.queryListMap(
							"select t.id, t.name, 1 as type,t.mobile, t.position from tb_teacher t,tb_classes c where (c.head_teacher = t.id or c.teachers like concat('%,',t.id,',%')) and c.stu_dept_id = "
									+ student.getDept_id());

			List<Map> mapList = Db.init().queryListMap(
					"select s.id,3 as type,s.mobile, s.name from tb_student s where s.dept_id = "
							+ student.getDept_id());

			teacherList.addAll(mapList);
			setAttr("personList", teacherList);
		}

			render("/weixin/contact/personlist.html");
			break;

		default:
			break;
		}

	}

	public void personList() {
		Integer id = getParaToInt("id", 0);
		Dept dept = Blade.create(Dept.class).findById(id);
		setAttr("deptName", dept.getFullname());
		if (dept.getType() == 1) {
			List<Map> mapList = Db.init().queryListMap(
					"select t.id,t.name,1 as type, t.position, t.mobile from tb_teacher t where is_in_dept(get_dept_childrens("
							+ id + "),t.depts) >0");

			setAttr("personList", mapList);
		} else if (dept.getType() == 2) {
			List<Map> teacherList = Db
					.init()
					.queryListMap(
							"select t.id, t.name, 1 as type, t.position, t.mobile from tb_teacher t,tb_classes c where (c.head_teacher = t.id or c.teachers like concat('%,',t.id,',%')) and c.parent_dept_id = "
									+ id);

			List<Map> mapList = Db
					.init()
					.queryListMap(
							"select p.id,2 as type, p.name,p.mobile, case p.position when 1 then '父亲' when 2 then '母亲' else '其他联系人' end as position,p.stu_name as sname from tb_parent p where p.dept_id = "
									+ id);

			teacherList.addAll(mapList);
			setAttr("personList", teacherList);
		} else if (dept.getType() == 3) {
			List<Map> teacherList = Db
					.init()
					.queryListMap(
							"select t.id, t.name, 1 as type,t.mobile, t.position from tb_teacher t,tb_classes c where (c.head_teacher = t.id or c.teachers like concat('%,',t.id,',%')) and c.stu_dept_id = "
									+ id);

			List<Map> mapList = Db.init().queryListMap(
					"select s.id,3 as type,s.mobile, s.name from tb_student s where s.dept_id = " + id);

			teacherList.addAll(mapList);
			setAttr("personList", teacherList);
		}

		render("/weixin/contact/personlist.html");
	}

	public void detail() {
		Integer id = getParaToInt("id", 0);
		Integer type = getParaToInt("type", 0);

		if (type != 2) {
			Map map = Db.init().queryMap(
					"select v.name,v.mobile,v.type,v.position from v_weixin_users v where v.id=" + id
							+ " and v.type = " + type, null);
			setAttr("person", map);
			// setAttr("userId", getUser().getId());
		} else {
			Map map = Db
					.init()
					.queryMap(
							"select p.name,p.mobile,p.stu_name as sname,case p.position when 1 then '父亲' when 2 then '母亲' else '其他联系人' end as position,2 type from tb_parent p where p.id="
									+ id, null);
			setAttr("person", map);
			// setAttr("userId", getUser().getId());
		}
		setAttr("myMobile", getUser().getMobile());
		WeixinApi.createQyChatJsToken(this);
		render("/weixin/contact/detail.html");
	}
}
